package cn.michael.dev.sort;

import java.util.Arrays;

/**
 * Created by hufenggang on 2021/10/21.
 * 选择排序
 */
public class SelectSort {

    /**
     * 每次选择最小的元素放在未排序的开头
     *
     * @param nums
     * @return
     */
    public static int[] sortArray(int[] nums) {
        int len = nums.length;
        for (int i = 0; i < len - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < len; j++) {
                if (nums[j] < nums[minIndex]) {
                    minIndex = j;
                }
            }
            swap(nums, i, minIndex);
        }

        return nums;
    }

    private static void swap(int[] nums, int index1, int index2) {
        int temp = nums[index1];
        nums[index1] = nums[index2];
        nums[index2] = temp;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{9, 4, 6, 8, 3, 10, 4, 6};
        sortArray(arr);
        System.out.println(Arrays.toString(arr));
    }
}
